//------------------------------------------------------------------------------
//@author czhou 2012 
//------------------------------------------------------------------------------

package mapengine {

import flash.display.MovieClip;
import flash.system.ApplicationDomain;

import framework.core.AssetsManager;
import framework.core.UIWidget;

import isoengine.IsoSetting;
import isoengine.world.IsoMapBg;
import isoengine.world.IsoMapBgSelect;
import isoengine.world.IsoWorld;

import mapengine.building.MapObjectBase;
import mapengine.building.MapStaticObject;
import mapengine.interfaces.IIsoMap;
import mapengine.interfaces.IMapBuilding;

import values.item.BuildingItemValue;
import values.map.MapObjectValue;
import values.map.MapValue;

public class IsoMap extends IsoWorld implements IIsoMap {
    //==========================================================================
    //  Properties 
    //==========================================================================
    
    protected var appDomain:ApplicationDomain;
    
    protected var data:MapValue;
    
    //==========================================================================
    //  Constructor 
    //==========================================================================
    
    public function IsoMap() {
        
        super();
    
    }
    
    //==========================================================================
    //  Protected methods 
    //==========================================================================
    
    protected function initStaticObject():void {
        
        for each(var vo:MapObjectValue in data.objects) {
            vo.objectClassRef = appDomain.getDefinition(vo.classRef) as Class;
            
            var obj:MapObjectBase = new MapStaticObject();
            obj.objectData = vo;
            obj.initialize();
            
            this.addSprite(obj);
        }
    
    }	
    
    //==========================================================================
    //  Public methods 
    //==========================================================================
    
    override public function initialize(lib:ApplicationDomain, value:*):void {
        
        data = value as MapValue;
        IsoSetting.cellX = data.width;
        IsoSetting.cellY = data.height;
        IsoSetting.lock = data.lock;
        appDomain = IsoSetting.appDomain = lib;
        bgTileClassRef = data.tileClassRef;
        
        super.init();		
        
        initStaticObject();
    
    }	
	
	/**
	 * 使用 BuildingItemValue 数据在地图上创建建筑 
	 * @param value
	 * @return 
	 * 
	 */	
	public function createBuilding(value:BuildingItemValue):IMapBuilding
	{
		var ref:Class = value.mapObjectClassRef;
		var building:IMapBuilding = new ref() as IMapBuilding;
		building.objectData = value;
		building.initialize();		
		addSprite(building);
		
		return building;
	}
	
	
}
}



